package forward;

import forward.api.EventHandler;
import forward.api.Events.World.EventPreUpdate;
import forward.api.Value.Value;
import forward.Manager.CommandManager;
import forward.Manager.FileManager;
import forward.Manager.FriendManager;
import forward.Manager.ModuleManager;
import forward.Module.Module;
import forward.Module.Modules.Render.TabUI;
import forward.UI.ClientNotification;
import forward.UI.Login.AltManager;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import net.minecraft.util.ResourceLocation;
import org.lwjgl.opengl.Display;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiChat;
import net.minecraft.network.play.client.C09PacketHeldItemChange;
import viamcp.ViaMCP;

public class Client {
    public final static String name = "forward";
    public final static String fuck = "| Code By Huiwow&LSM. ";
    public final static String ver = "1.2.2";
    public static boolean publicMode = false;
    public static Client instance = new Client();
    public static boolean passa;
    public static boolean passb;
    public static boolean paiduser;
    public static ModuleManager modulemanager;
  //  public static ResourceLocation CLIENT_CAPE = new ResourceLocation("SkidAssets/CAPE/bengbu.png");
    private static ArrayList<ClientNotification> notifications = new ArrayList<>();
    public String username;
    private CommandManager commandmanager;
    private AltManager altmanager;
    private FriendManager friendmanager;
    private TabUI tabui;

    public static String sendGet(final String url) {
        String result = "";
        try {
            final String urlNameString = url;
            final URL realurl = new URL(urlNameString);
            HttpURLConnection httpUrlConn = (HttpURLConnection) realurl.openConnection();
            httpUrlConn.setDoInput(true);
            httpUrlConn.setRequestMethod("GET");
            httpUrlConn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt) Huiwow&LSM IS God");
            InputStream input = httpUrlConn.getInputStream();
            InputStreamReader read = new InputStreamReader(input);
            BufferedReader br = new BufferedReader(read);
            String data = br.readLine();
            while (data != null) {
                result = String.valueOf(result) + data + "\n";
                data = br.readLine();
            }
            br.close();
            read.close();
            input.close();
            httpUrlConn.disconnect();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static ModuleManager getModuleManager() {
        return modulemanager;
    }

    public void initiate() {
        this.commandmanager = new CommandManager();
        this.commandmanager.init();
        this.friendmanager = new FriendManager();
        this.friendmanager.init();
        this.modulemanager = new ModuleManager();
        this.modulemanager.init();
        this.tabui = new TabUI();
        this.tabui.init();
        this.altmanager = new AltManager();
        AltManager.init();
        AltManager.setupAlts();
        FileManager.init();
        Display.setTitle(this.name + " " + this.ver + " " + this.fuck + "");
        try
        {
            ViaMCP.getInstance().start();

            // Only use one of the following
            ViaMCP.getInstance().initAsyncSlider(); // For top left aligned slider
            //ViaMCP.getInstance().initAsyncSlider(x, y, width (min. 110), height (recommended 20)); // For custom position and size slider
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public CommandManager getCommandManager() {
        return this.commandmanager;
    }

    public AltManager getAltManager() {
        return this.altmanager;
    }

    public void shutDown() {
        String values = "";
        instance.getModuleManager();
        for (Module m : ModuleManager.getModules()) {
            for (Value v : m.getValues()) {
                values = String.valueOf(values)
                        + String.format("%s:%s:%s%s", m.getName(), v.getName(), v.getValue(), System.lineSeparator());
            }
        }
        FileManager.save("Values.txt", values, false);
        String enabled = "";
        instance.getModuleManager();
        for (Module m : ModuleManager.getModules()) {
            if (!m.isEnabled())
                continue;
            enabled = String.valueOf(enabled) + String.format("%s%s", m.getName(), System.lineSeparator());
        }
        FileManager.save("Enabled.txt", enabled, false);
    }

    @EventHandler
    public void onUpdate(EventPreUpdate event) {
        if (Minecraft.currentScreen != null && !(Minecraft.currentScreen instanceof GuiChat)) {
            Minecraft.thePlayer.sendQueue.addToSendQueue(new C09PacketHeldItemChange(6));
        }
    }

}
